package levels;

import gameobjects.*;
import resources.Music;
import tools.GameApplet;
import abstracts.Level;
import land.Land;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class XmlLevel extends Level {
    private final String FILENAME;

    public class Handler extends DefaultHandler {
        private int row = 0;
        private int col = 0;
        private Level LEVEL;

        public Handler(Level level) {
            LEVEL = level;
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes atts) throws SAXException {
            if (localName == "cell") {
                String type = atts.getValue("type");
                if (type.contentEquals("floor")) {
                    LEVEL.getCollisionGrid().set(row, col, true, false, false);
                } else if (type.contentEquals("wall")) {
                    LEVEL.getCollisionGrid().set(row, col, true, true, true);
                } else if (type.contentEquals("mage")) {
                    add(new Mage(LEVEL, row, col));
                    LEVEL.getCollisionGrid().set(row, col, true, false, false);
                }
                /*
                 * else if (type.contentEquals("diamond")) { add(new
                 * Diamond(LEVEL, row, col));
                 * LEVEL.COLLISION_GRID.set(row, col, true, false,
                 * false); } else if (type.contentEquals("ruby")) {
                 * add(new Ruby(LEVEL, row, col));
                 * LEVEL.COLLISION_GRID.set(row, col, true, false,
                 * false); } else if (type.contentEquals("emerald")) {
                 * add(new Emerald(LEVEL, row, col));
                 * LEVEL.COLLISION_GRID.set(row, col, true, false,
                 * false); } else if (type.contentEquals("sapphire"))
                 * { add(new Sapphire(LEVEL, row, col));
                 * LEVEL.COLLISION_GRID.set(row, col, true, false,
                 * false); } else if
                 * (type.contentEquals("eyeball-spawn")) { add(new
                 * EyeballSpawn(LEVEL, row, col));
                 * LEVEL.COLLISION_GRID.set(row, col, true, false,
                 * false); }
                 */
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) {
            if (localName == "cell") {
                col++;
            } else if (localName == "row") {
                col = 0;
                row++;
            }
        }

        @Override
        public void startPrefixMapping(String prefix, String uri)
                throws SAXException {
        }
    }

    public XmlLevel(GameApplet applet, String filename) {
        super(applet);
        FILENAME = filename;
    }

    @Override
    public void load() {
        super.begin();

        try {
            XMLReader parser = XMLReaderFactory.createXMLReader();
            parser.setContentHandler(new Handler(this));
            parser.parse("xml-levels/" + FILENAME);
        } catch (Exception e) {
            e.printStackTrace();
        }

        new Land(getCollisionGrid(), this);

        Music.set(Music.BACKGROUND_1);
    }
}
